package com.cheng.xbsc.controller;

import com.cheng.xbsc.entity.Car;
import com.cheng.xbsc.entity.vo.Result;
import com.cheng.xbsc.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: 刘文铖
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {


    @Autowired
    private GoodsService goodsService;

    /**
     * 根据商品id查询详情
     *
     * @param goodsId
     * @return
     */
    @GetMapping("/findDetail/{goodsId}")
    public Result findDetail(@PathVariable("goodsId") Integer goodsId) {
        return goodsService.findDetail(goodsId);
    }

    /**
     * 收藏或取消收藏
     *
     * @param goodsId
     * @return
     */
    @GetMapping("/doCollect/{goodsId}")
    public Result doCollect(@PathVariable("goodsId") Integer goodsId) {
        return goodsService.doCollect(goodsId);
    }

    /**
     * 添加倒购物车
     *
     * @param car
     * @return
     */
    @PostMapping("/addCar")
    public Result addCar(@RequestBody Car car) {
        goodsService.saveCar(car);
        return new Result(true, "加入购物车成功");
    }

    /**
     * 根据列表查询
     *
     * @param pageNum
     * @param productId
     * @return
     */
    @GetMapping("/findProduct/{pageNum}/{productId}")
    public Result findProduct(@PathVariable("pageNum") Integer pageNum, @PathVariable("productId") Integer productId) {
        return new Result(true, "查询成功", goodsService.findProduct(pageNum, productId));
    }

    /**
     * 搜索查询
     *
     * @param pageNum
     * @param title
     * @return
     */
    @GetMapping("/searchTitle/{pageNum}")
    public Result searchTitle(@PathVariable("pageNum") Integer pageNum, String title) {
        return new Result(true, "查询成功", goodsService.searchTitle(pageNum, title));
    }

    /**
     * 查询收藏商品
     */
    @GetMapping("/findCollect/{pageNum}")
    public Result findCollect(@PathVariable("pageNum") Integer pageNum) {
        return new Result(true, "查询成功", goodsService.findCollect(pageNum));
    }
}
